/* CommandLine InfoPath 2007 URN Changer
 * Copyright (C) 2008-2009 Michael Stum
 * http://www.Stum.de
 * http://cmdtools.codeplex.com
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, Version 2, June 1991.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can find it on the GNU Website:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace stumde.cmdtools.cmdxsnurn
{
    public class Arguments
    {
        public string NewUrn { get; set; }
        public bool NoBackup { get; set; }
        public bool ExtractTemplateXml { get; set; }
        public bool Help { get; set; }

        private readonly Dictionary<string, List<string>> _cmdParams;
        public Dictionary<string, List<string>> CmdParams
        {
            get
            {
                return _cmdParams;
            }
        }

        /// <summary>
        /// Sets the internal Property to the given Value
        /// </summary>
        /// <param name="propertyName">The case-sensitive Name of the property to set</param>
        /// <param name="newValue">The new Value</param>
        /// <param name="index">For indexed Types: The index to set, otherwise leave null</param>
        private void SetProperty(string propertyName, object newValue, object[] index)
        {
            var pi = GetType().GetProperty(propertyName);
            if (pi != null)
            {
                pi.SetValue(this, newValue, index);
            }
            else
            {
                throw new ArgumentException(string.Format("Cannot find Property \"{0}\" in class \"{1}\"", propertyName, GetType().FullName));
            }
        }

        /// <summary>
        /// Sets a Boolean Property - if any of the arguments in argumentsNames is set, sets the property to true
        /// </summary>
        /// <param name="propertyName">The case-sensitive name of the property to set</param>
        /// <param name="argumentNames">The command line arguments to check</param>
        private void SetBoolProperty(string propertyName, params string[] argumentNames)
        {
            bool parameter = false;
            foreach (string s in argumentNames)
            {
                if (CmdParams.ContainsKey(s))
                {
                    parameter = true;
                    break;
                }
            }
            SetProperty(propertyName, parameter, null);
        }

        /// <summary>
        /// Sets a string property
        /// </summary>
        /// <param name="propertyName">The case-sensitive name of the property to set</param>
        /// <param name="separator">If any of the arguments is specified multiple times, the resulting string will use this as a separator</param>
        /// <param name="argumentNames">The command line arguments to check</param>
        /// <returns>An integer, specifying how many command line arguments matched the argumentNames</returns>
        private void SetStringProperty(string propertyName, string separator, params string[] argumentNames)
        {
            var sb = new StringBuilder();
            foreach (string s in argumentNames)
            {
                if (CmdParams.ContainsKey(s) && CmdParams[s] != null)
                {
                    for (int i = 0; i < CmdParams[s].Count; i++)
                    {
                        if (sb.Length > 0) sb.Append(separator);
                        sb.Append(CmdParams[s][i]);
                    }
                }
            }
            SetProperty(propertyName, sb.ToString(), null);
        }

        /// <summary>
        /// The Constructor - transform the Dictionary into properties of this class
        /// </summary>
        /// <param name="cmdParams"></param>
        public Arguments(Dictionary<string, List<string>> cmdParams)
        {
            _cmdParams = cmdParams;

            SetBoolProperty("Help", "?", "help");
            SetBoolProperty("NoBackup", "n", "nobackup");
            SetBoolProperty("ExtractTemplateXml", "t", "template");
            SetStringProperty("NewUrn", string.Empty, "u", "urn");
        }

    }
}
